//
//  YCOrderSearchViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/28.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCOrderSearchViewController.h"
#define YCOrderSearchCellIdentify @"YCOrderSearchTableViewCell"

@interface YCOrderSearchViewController ()
{
    UITextField *m_searchTF;
    UITableView *m_tableView;
    
    NSInteger _currentPage;
    NSMutableArray *m_arrayList;
}
@end

@implementation YCOrderSearchViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:NO];
    [_naviVC setMainNaviVC];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem=[self tbarBackButtonWhite];
    
    _currentPage=1;
    m_arrayList=[NSMutableArray array];
    [self addMainView];
    [[App shared]readShopData];
}
-(void)loadData
{
    OrderIndexRequest *request = [OrderIndexRequest new];
    [apiClient hideProgress];
    //[apiClient disableAfterRequest];
    request.shop_id=[App shared].shop.Id;
    request.search=m_searchTF.text;
    request.status=@"9";
    request.page=[NSString stringWithFormat:@"%ld",(long)_currentPage];
    [apiClient doOrderIndex:request success:^(ApiResponse *data, NSString *url) {
        OrderIndexResponse *response=[[OrderIndexResponse alloc]initWithCGResponse:data];
        if (_currentPage>1) {
            [m_arrayList addObjectsFromArray:response.list];
        }else{
            m_arrayList=[NSMutableArray arrayWithArray:response.list];
        }
        [m_emptyView setHidden:!(m_arrayList.count==0)];
        [m_tableView reloadData];
        
        if (response.list.count==0) {
            m_tableView.mj_footer.state=MJRefreshStateNoMoreData;
        }else{
            [m_tableView.mj_footer endRefreshing];
        }
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
-(void)addMainView
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kNAV_HEIGHT)];
    
    m_searchTF=[[UITextField alloc]initWithFrame:CGRectMake(kDistance/2, 17, kMainScreen_Width-60-40,30)];
    UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kDistance, 30)];
    m_searchTF.leftView = leftView;
    m_searchTF.leftViewMode = UITextFieldViewModeAlways;
    
    m_searchTF.layer.cornerRadius = 2.0f;
    m_searchTF.clearButtonMode=UITextFieldViewModeWhileEditing;
    [m_searchTF setTextColor:TEXT_MIDDLE];
    [m_searchTF setPlaceholder:@"手机号/订单号" Corlor:DISABLE_COLOR];
    [m_searchTF setFont:[UIFont LightFontOfSize:14]];
    m_searchTF.backgroundColor = WHITE_COLOR;
    m_searchTF.delegate=self;
    m_searchTF.returnKeyType=UIReturnKeySearch;
    [m_searchTF becomeFirstResponder];
    
    [titleView addSubview:m_searchTF];
    self.navigationItem.titleView=titleView;
    
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn.titleLabel setFont:[UIFont LightFontOfSize:15]];
    [searchBtn addTarget:self action:@selector(btnSearchClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    
    
    m_tableView=[[UITableView alloc]init];
    m_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    m_tableView.delegate=self;
    m_tableView.dataSource=self;
    [self.view addSubview:m_tableView];
    [m_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(0);
    }];
    [m_tableView registerNib:[UINib nibWithNibName:YCOrderSearchCellIdentify bundle:nil] forCellReuseIdentifier:YCOrderSearchCellIdentify];

    UIView *header=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kSmallDistance)];
    m_tableView.tableHeaderView=header;
    
    m_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage=1;
        [self loadData];
        [m_tableView.mj_header endRefreshing];
    }];
    m_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _currentPage++;
        [self loadData];
    }];
    
    m_emptyView=[[YCEmptyView alloc]initWithFrame:CGRectMake(0, kNAV_HEIGHT, W(self.view), kMainPage_Height)];
    [m_emptyView setEmptyWith:@"public_nodata" Title:@"没有找到相应的订单"];
    [self.view addSubview:m_emptyView];
}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return m_arrayList.count;
}
#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderTable *order=m_arrayList[indexPath.section];
    CGFloat height=getTextSize(order.item_name, kMainScreen_Width-80, [UIFont LightFontOfSize:17]).height+getTextSize(order.addr_address, kMainScreen_Width-184, [UIFont LightFontOfSize:14]).height;
    return 210-39+height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    YCOrderSearchTableViewCell *_cell = (YCOrderSearchTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCOrderSearchCellIdentify forIndexPath:indexPath];
    [_cell bindWith:m_arrayList[indexPath.row]];
    cell=_cell;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YCOrderDetailViewController *detailVC=[[YCOrderDetailViewController alloc]init];
    OrderTable *order=m_arrayList[indexPath.row];
    detailVC.orderId=order.order_no;
    [self showNavigationView:detailVC];
}

-(void)btnSearchClicked:(UIButton *)button
{
    [self loadData];
    [m_searchTF resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self loadData];
    return YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [m_searchTF resignFirstResponder];
}

@end





